package com.example.admin.midtermpro41;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.recyclerview.adapters.ScaleInAnimationAdapter;
import jp.wasabeef.recyclerview.animators.OvershootInLeftAnimator;


public class MainActivity extends AppCompatActivity {
    public static QuickAdapter heroAdapter;//用于主页面显示英雄卡牌
    public static QuickAdapter searchAdapter;//用于查询显示
    public static List<hero>[] heroList=new List[]{new ArrayList<>()};//[0]用于缓存英雄信息
    public static List<String>[] searchList=new List[]{new ArrayList<>()};//[0]用于缓存查询列表的信息
    public static RecyclerView heroRecyclerView;
    public static RecyclerView searchRecyclerView;
    public static Toolbar toolbar;
    public static AlertDialog.Builder heroAdapterBuilder,searchAdapterBuilder;
    public static int OnDeletingItemPosition,OnAddingItenPosition;
    public static hero[] heroes;
    public static Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        toolbar = findViewById(R.id._toolbar);
        heroAdapterBuilder = new AlertDialog.Builder(MainActivity.this);
        searchAdapterBuilder=new AlertDialog.Builder(MainActivity.this);
        context=MainActivity.this;
        setSupportActionBar(toolbar);
        hero.initHeroList(heroList);

        //--------------------------------recyclerView设置开始--------------------------------------
        /*下面是主页面的英雄列表初始化*/
        heroAdapter=new QuickAdapter<hero>(MainActivity.heroList[0]) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.hero_list_layout;
            }
            @Override
            public void convert(VH holder, hero data, int position) {
                holder.setText(R.id.heroName,data.heroName);
                holder.setText(R.id.heroATK,"攻击值："+data.heroATK);
                holder.setText(R.id.heroDEF,"防御值:"+data.heroDEF);
                holder.setImage(R.id.heroProtrait,data.headPortraitID);
            }
        };
        heroRecyclerView = findViewById(R.id.heroRecyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this );
        heroRecyclerView.setLayoutManager(layoutManager);
        layoutManager.setOrientation(OrientationHelper. VERTICAL);
        ScaleInAnimationAdapter scaleInAnimationAdapter = new ScaleInAnimationAdapter(heroAdapter);
        scaleInAnimationAdapter.setDuration(1000);
        heroRecyclerView.setAdapter((scaleInAnimationAdapter));
        heroRecyclerView.setItemAnimator(new OvershootInLeftAnimator());
        /*下面初始化搜索框列表*/
        searchAdapter=new QuickAdapter<String>(MainActivity.searchList[0]) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.search_list_layout;
            }
            @Override
            public void convert(VH holder, String data, int position) {
                holder.setText(R.id.searchName,data);
                if(hero.searchHeroIsInHeroListOrNot(data)){//查到，不加加号
                    holder.setText(R.id.isFoundInHeroList,"已添加");
                }else{
                    holder.setText(R.id.isFoundInHeroList,"点击添加");
                }
            }
        };
        searchRecyclerView=findViewById(R.id.searchRecyclerView);
        searchRecyclerView.setVisibility(View.INVISIBLE);
        LinearLayoutManager searchLayoutManager = new LinearLayoutManager(this );
        searchRecyclerView.setLayoutManager(searchLayoutManager);
        searchLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        ScaleInAnimationAdapter searchScaleInAnimationAdapter = new ScaleInAnimationAdapter(searchAdapter);
        searchScaleInAnimationAdapter.setDuration(1000);
        searchRecyclerView.setAdapter(searchScaleInAnimationAdapter);
        searchRecyclerView.setItemAnimator(new OvershootInLeftAnimator());
        //--------------------------------recyclerView设置结束--------------------------------------

        /*增加事件监听器*/
        eventListerner.initHeroAdapterEventLister(heroAdapter);
        eventListerner.initSearchAdapter(searchAdapter);


    }

    /*下面的是搜索框的相关设置*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search_view, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);//找到searchView
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        SearchView.SearchAutoComplete mSearchAutoComplete = searchView.findViewById(R.id.search_src_text);//设置输入框提示文字样式
        mSearchAutoComplete.setHintTextColor(getResources().getColor(android.R.color.white));//设置提示文字颜色
        mSearchAutoComplete.setTextColor(getResources().getColor(android.R.color.white));//设置内容文字颜色
        searchView.setQueryHint("输入英雄名查找");//设置默认无内容时的文字提示
        /*给搜索输入框增加事件监听器*/
        eventListerner.initQueryTextListener(searchView);
        eventListerner.initOnCloseListener(searchView);
        return super.onCreateOptionsMenu(menu);
    }
}